package com.bluesmart.mia;

import java.util.TimeZone;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sys.DemoApp;
import com.umeng.message.UmengRegistrar;
import com.util.Constant;
import com.util.MgqDataHandler;
import com.util.MgqRestClient;
import com.util.PreferencesUtils;
import com.util.ToastUtil;
import com.util.Utils;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class LoginActivity extends BaseActivity implements OnClickListener
{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		((TextView) findViewById(R.id.center)).setText(getString(R.string.sign_in));
		((Button) findViewById(R.id.right))
				.setCompoundDrawablesWithIntrinsicBounds(0, 0,
						R.drawable.a01, 0);
		
		findViewById(R.id.left).setOnClickListener(this);
		findViewById(R.id.right).setOnClickListener(this);
		findViewById(R.id.forget).setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {
		if(R.id.left == arg0.getId()) finish();
		else if(R.id.right == arg0.getId())
		{
			sendHttpRequest();
		}
		else if(R.id.forget == arg0.getId())
		{
			startActivity(new Intent(this,ForgetActivity.class));
		}
	}
	
	public void sendGetBabyRequest() {
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {
				((DemoApp)getApplicationContext()).mBabyList = JSON.parseArray(response);
				if(((DemoApp)getApplicationContext()).mBabyList != null)
				{
					int count = ((DemoApp)getApplicationContext()).mBabyList.size();
					for(int i = 0; i < count; i++)
					{
						JSONObject item = ((DemoApp)getApplicationContext()).mBabyList.getJSONObject(i);
						if(item.getString("isCurr").equals("true")) ((DemoApp)getApplicationContext()).mSelectedBaby = i;
					}
					
					startActivity(new Intent(LoginActivity.this,BabyMilkBottleActivity.class));
				}
			}
		};
		
		String username = PreferencesUtils.getString(this, "username");
		String password = PreferencesUtils.getString(this, "password");
		if(username == null || username.length() == 0)
		{
			username = PreferencesUtils.getString(this, "username_tmp");
			password = PreferencesUtils.getString(this, "password_tmp");
		}
		
		JSONObject params = new JSONObject();
		params.put("mia_un",username);
		params.put("pwd", password);
		params.put("handType", "get");

		MgqRestClient.postBody(Constant.SERVER_URL + "/sw/babyInfoHandler.ashx", JSON.toJSONString(params), loginHandler);
	}
	
	private void sendHttpRequest() {
		final String email = ((EditText)findViewById(R.id.email)).getText().toString();
		final String pwd = ((EditText)findViewById(R.id.pwd)).getText().toString();
		
		if(email.length() == 0 || pwd.length() == 0)
		{
			ToastUtil.showToastWithAlertPic(R.string.sign_in_tip);
			return;
		}
		
		if(!Utils.isEmail(email) && !Utils.isMobileNO(email))
		{
			ToastUtil.showToastWithAlertPic(R.string.sign_in_email);
			return;
		}
		
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {
				JSONObject ruser = JSON.parseObject(response);

				if (ruser.getString("resCode").equals("1000")) {
					PreferencesUtils.putString(getApplicationContext(), "username_tmp", email);
					PreferencesUtils.putString(getApplicationContext(), "password_tmp", Utils.Md5(pwd));
					PreferencesUtils.putString(getApplicationContext(), "accessToken", ruser.getString("accessToken"));
					
					String jump = ruser.getString("jump");
					if(jump.equals("milk"))
					{
						sendGetBabyRequest();
					}
					else if(jump.equals("choice"))
					{
						startActivity(new Intent(LoginActivity.this,AddDeviceActivity.class));
					}
					else if(jump.equals("addbaby"))
					{
						startActivity(new Intent(LoginActivity.this,AddDeviceActivity.class));
					}
					else if(jump.equals("apply"))
					{
						startActivity(new Intent(LoginActivity.this,ApplyActivity.class));
					}
					else
					{
						PreferencesUtils.putString(getApplicationContext(), "is_login", "1");
						PreferencesUtils.putString(getApplicationContext(), "nickname", ruser.getString("nickname"));
						PreferencesUtils.putString(getApplicationContext(), "username", email);
						PreferencesUtils.putString(getApplicationContext(), "password", Utils.Md5(pwd));
						PreferencesUtils.putString(getApplicationContext(), "password_src", pwd);
						PreferencesUtils.putString(getApplicationContext(), "userimg", ruser.getString("userimg"));
						
						startActivity(new Intent(LoginActivity.this,MainTabActivity.class));
					}
				}
				else if (ruser.getString("resCode").equals("2000")) {
					ToastUtil.showToastWithAlertPic(R.string.error_2000);
				}
				else if (ruser.getString("resCode").equals("3000")) {
					ToastUtil.showToastWithAlertPic(R.string.error_3000);
				}
				else 
				{
				}
			}
		};
		
		JSONObject params = new JSONObject();
		params.put("userName", email);
		params.put("pwd", Utils.Md5(pwd));
		params.put("tid", "");
		params.put("timezone", TimeZone.getDefault().getRawOffset()/(3600*1000));
		params.put("deviceid", UmengRegistrar.getRegistrationId(this));

		MgqRestClient.postBody(Constant.SERVER_URL + "/sw/login.ashx", JSON.toJSONString(params), loginHandler);
	}
}
